import { Coupon } from './coupon-model.js';
var util = require ('../../utils/util.js');
var coupon = new Coupon();
Page({

  data: {
    pageIndex: 1,
    loadingHidden: true,
    loadMore: true,
    dataArr: [],
    showShare: false
  },
  onLoad: function(options) {
    this.data.shop_id = options.shop_id ? options.shop_id : 1
    this._getData();
  },
  /*获取优惠券列表*/
  _getData: function() {
    var that = this;
    var args = {
      page: this.data.pageIndex,
      number:10,
      shop_id: that.data.shop_id
    }
    that.setData({
      loadingHidden: false
    })
    coupon.getTypeList(args, (res) => {
      var items = res.items;
      if (!items || items.length < res._meta.perPage) {
        this.data.loadMore = false;
      }
      items = that.data.dataArr.concat(items);
      that.setData({
        dataArr: items,
        loadingHidden: true,
        searchLoading: false,
        searchLoadingComplete: false,
      });
    });
  },
  /*领取优惠券*/
  receive: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var args = {
      coupon_id: id,
    }
    coupon.receive(args, (res) => {
      if(res.status == 1){
        wx.showToast({
          title: '领取成功',
          icon: 'success',
          duration: 2000
        })
      }
      else{
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (!loadMore) {
      return false;
    }
    this.data.pageIndex + 1;
    this._getData();
  },
  //自定义底部跳转
  footRedirect: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.switchTab({
      url: url,
    })
  },
    //跳转到卡券详情
  toMy: function (e) {
    wx.navigateTo({
      url: 'my'
    })
  },
})